package cc.aiee.demo.springboot.controller.bambora;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.beanstream.responses.PaymentResponse;

import cc.aiee.bambora.northamerica.api.BamboraPayApi;
import cc.aiee.bambora.northamerica.model.BamboraCardPurchaseModel;
import cc.aiee.bambora.northamerica.model.BamboraRefundModel;
import cc.aiee.bambora.northamerica.model.BamboraTokenPurchaseModel;
import cc.aiee.core.bean.R;
import cc.aiee.core.kit.KitId;
import cc.aiee.demo.springboot.basic.constant.ContentType;
import cc.aiee.demo.springboot.basic.constant.ResultConst;
import cc.aiee.demo.springboot.controller.bambora.bean.BamboraCardQuery;
import cc.aiee.demo.springboot.controller.bambora.bean.BamboraQuery;
import cc.aiee.demo.springboot.controller.bambora.bean.BamboraRefundQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/bambora")
@Tag(name = "Bambora支付")
@Slf4j
public class BamboraPayController {

	@PostMapping(value = "/purchase", consumes = { ContentType.FORM })
	@Operation(summary = "购买")
	public @ResponseBody String payments(BamboraQuery query) {
		log.info("Bambora Pay,请求参数:\n{}", query.toJson(true, false));
		BamboraPayApi api = new BamboraPayApi();

		BamboraTokenPurchaseModel model = query.model(new BamboraTokenPurchaseModel());

		model.setOrderNo(KitId.timeId());

		boolean purchase = api.purchase(model);

		return purchase ? ResultConst.SUCCESS : ResultConst.FAIL;
	}

	@PostMapping(value = "/card", consumes = { ContentType.FORM })
	@Operation(summary = "信用卡购买")
	public @ResponseBody R<PaymentResponse> payments(BamboraCardQuery query) {
		BamboraPayApi api = new BamboraPayApi();

		BamboraCardPurchaseModel model = query.model(new BamboraCardPurchaseModel());

		model.setOrderNo(KitId.timeId());

		return api.cardPayment(model);

	}

	@PostMapping(value = "/void", consumes = { ContentType.FORM })
	@Operation(summary = "作废交易(只能在原始交易的同一天尝试作废)")
	public @ResponseBody R<PaymentResponse> void2(BamboraRefundQuery query) {
		BamboraPayApi api = new BamboraPayApi();

		BamboraRefundModel model = query.model(new BamboraRefundModel());

		return api.voidPayment(model);

	}

	@PostMapping(value = "/refund", consumes = { ContentType.FORM })
	@Operation(summary = "退款")
	public @ResponseBody R<PaymentResponse> refund(BamboraRefundQuery query) {
		BamboraPayApi api = new BamboraPayApi();

		BamboraRefundModel model = query.model(new BamboraRefundModel());

		return api.refund(model);
	}
}
